package com.huike.review.demo.minIO;

import ch.qos.logback.core.net.server.Client;
import io.minio.*;
import io.minio.errors.*;
import sun.security.krb5.Credentials;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @Author: cuiliangbo
 * @Date: 2022/5/27 11:59
 */
@SuppressWarnings("all")
public class MinIOTest {

    public static void main(String[] args) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {

        String ak = "minioadmin";
        String sk = "minioadmin";
        String endpoint = "http://127.0.0.1:9000";
        String bucket = "test";

        MinioClient minioClient = getMinioClient(endpoint,ak,sk);

        boolean exits = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if (!exits){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).region("test").build());
        }

        File file = new File("C:\\Users\\Boy_Cui\\Desktop\\1.png");
        String fileName = file.getName();
        InputStream inputStream = new FileInputStream(file);
        try {
            minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object("2.png").stream(inputStream,inputStream.available(),-1).contentType("image/png").build());
        } catch (MinioException e) {
            e.printStackTrace();
        }


    }

    public static MinioClient getMinioClient(String endpoint,String ak,String sk){
        return MinioClient.builder().endpoint(endpoint).credentials(ak,sk).build();
    }
}
